package cn.edu.tju.elm.service;

import cn.edu.tju.elm.model.WalletTransaction;
import cn.edu.tju.elm.model.TransactionType;
import cn.edu.tju.elm.repository.WalletTransactionRepository;
import cn.edu.tju.elm.service.WalletTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class WalletTransactionServiceImpl implements WalletTransactionService {

    @Autowired
    private WalletTransactionRepository transactionRepository;

    @Override
    public WalletTransaction getTransactionById(Long transactionId) {
        return transactionRepository.findById(transactionId)
                .orElseThrow(() -> new IllegalArgumentException("交易记录不存在"));
    }

    @Override
    public WalletTransaction getTransactionByReferenceId(String referenceId) {
        return transactionRepository.findByReferenceId(referenceId)
                .orElseThrow(() -> new IllegalArgumentException("交易记录不存在"));
    }

    @Override
    public BigDecimal getTotalIncome(Long walletId) {
        return transactionRepository.getTotalIncome(walletId);
    }

    @Override
    public BigDecimal getTotalExpense(Long walletId) {
        return transactionRepository.getTotalExpense(walletId);
    }

    @Override
    public List<WalletTransaction> getTransactionsByType(Long walletId, TransactionType transactionType) {
        return transactionRepository.findByWalletIdAndTransactionTypeOrderByCreateTimeDesc(walletId, transactionType);
    }
}